package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.Employee;
import com.example.entity.User;
import com.example.service.EmployeeService;
import com.example.util.R;
import org.apache.catalina.connector.Request;
import org.apache.catalina.session.StandardSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * 员工信息 前端控制器
 * </p>
 *
 * @author bd
 * @since 2023-05-27
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;


    @PostMapping("/login")
    public R<Employee> login(@RequestBody  Employee employee,HttpServletRequest request)
    {
        System.out.println(employee.toString());
        String password= DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());//先把输入的密码MD5加密
        //不加@RequestBody 的话 输出结果
 //Employee(id=null, name=null, username=null, password=null, phone=null, sex=null, idNumber=null, status=null, createTime=null, updateTime=null, createUser=null, updateUser=null)

        LambdaQueryWrapper<Employee> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername,employee.getUsername());
        Employee employee1 = employeeService.getOne(wrapper);

        if(employee1==null)
        {
            return R.error("用户不存在");
        }
        if(!employee1.getPassword().equals(password)) //MD5判断
        {
            return  R.error("密码错误");
        }
        if(employee1.getStatus()==0)
        {
            return R.error("用户被禁用");
        }

        request.getSession().setAttribute("employee",employee1.getId());
        return R.success(employee1);
    }


    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request)
    {
        request.getSession().removeAttribute("employee");
        return R.success("退出成功！");
    }

    //page?page=1&pageSize=10

    @GetMapping("/page")
    public R<Page> employeeList(int page,int pageSize,String  name)
    {

        Page<Employee> pageInfo=new Page<>(page,pageSize);

        LambdaQueryWrapper<Employee> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(!StringUtils.isEmpty(name),Employee::getName,name);
        lambdaQueryWrapper.orderByDesc(Employee::getCreateTime);

        employeeService.page(pageInfo,lambdaQueryWrapper);

        return R.success(pageInfo);
    }

    @PostMapping
    public R<String> addEmployee(@RequestBody Employee employee)
    {
        //employee.setPassword("123456");   //密码要给默认值，创建时间、...都要给默认值  由mymetaOjbectHandler处理
        //密码不能这样设置，要默认MD5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        boolean flag = employeeService.save(employee);
        if(flag)
        return R.success("添加成功");
        else
            return R.success("添加失败");
    }


    //编辑
  //1.数据回显
    @GetMapping("/{id}")
    public R<Employee> updataShow(@PathVariable Long id){

        Employee employee = employeeService.getById(id);
        if (employee!=null)
            return R.success(employee);
        return R.error("用户不存在");
    }

    //
         @PutMapping
        public R<String> updateEmplyee(@RequestBody Employee employee)
        {
            boolean b = employeeService.updateById(employee);
            if(b)
            return R.success("状态更新成功");
            else
                return R.error("更新失败");
        }
}
